﻿/*******************************************************************************
 * Copyright © 2016
 * 
 * Description: MVC快速开发平台  FROM http://xmwxgn.com
 *
*********************************************************************************/
using ZFine.Code;
using ZFine.Domain.Entity.Gas;
using ZFine.Domain.IRepository.Gas;
using ZFine.Repository.Gas;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ZFine.Application.Gas
{
    public class ResidentApp
    {
        private IResidentRepository service = new ResidentRepository();

        /// <summary>
        /// 客户端插入
        /// </summary>
        /// <param name="chargeRecordEntity"></param>
        public void ClientInsert(ResidentEntity residentEntity)
        {
            residentEntity.ClientCreate();
            service.Insert(residentEntity);
        }
        public List<ResidentEntity> GetList(Pagination pagination,string RName, string IDNumber,string StartDate,String EndDate)
        {
            var expression = ExtLinq.True<ResidentEntity>();
            if (!string.IsNullOrEmpty(RName))
            {
                expression = expression.And(t => t.RName.Contains( RName));
            }
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber.Contains(IDNumber));
            }
            if (!string.IsNullOrEmpty(StartDate))
            {
                DateTime start = DateTime.Parse(StartDate + " 00:00:00");
                expression = expression.And(t => t.FaceRecognizeTime >= start);
            }
            if (!string.IsNullOrEmpty(EndDate))
            {
                DateTime end = DateTime.Parse(EndDate + " 23:59:59");
                expression = expression.And(t => t.FaceRecognizeTime <= end);
            }
            return service.FindList(expression, pagination);
        }
        public List<ResidentEntity> GetList( string IDNumber = "")
        {
            var expression = ExtLinq.True<ResidentEntity>();
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber == IDNumber);
            }
            return service.IQueryable(expression).OrderBy(t => t.IDNumber).ToList();
        }
        public List<ResidentEntity> GetListByPlateNumber(string IDNumber)
        {
            var expression = ExtLinq.True<ResidentEntity>();
           expression = expression.And(t => t.IDNumber == IDNumber);
            return service.IQueryable(expression).OrderBy(t => t.IDNumber).ToList();
        }
        /// <summary>
        /// 获得IQueryable
        /// </summary>
        /// <param name="IDNumber"></param>
        /// <returns></returns>
        public IQueryable<ResidentEntity> GetIQueryable()
        {
            return service.IQueryable();
        }
        public IQueryable<ResidentEntity> GetIQueryable(string IDNumber = "")
        {
            var expression = ExtLinq.True<ResidentEntity>();
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber == IDNumber);
            }
            return service.IQueryable(expression);
        }
        public int GetCount()
        {
            return service.IQueryable().Count();
        }
        public ResidentEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public ResidentEntity GetFormByIDNumber(string IDNumber)
        {
            var expression = ExtLinq.True<ResidentEntity>();
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber == IDNumber);
            }
            return service.FindEntity(expression);
        }
        //提交
        public void SubmitForm(ResidentEntity organizeEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                organizeEntity.Modify(keyValue);
                service.Update(organizeEntity);
            }
            else
            {
                organizeEntity.Create();
                service.Insert(organizeEntity);
            }
        }
        //删除
        public void Del(string keyValue)
        {
            service.Delete(a=>a.F_Id==keyValue);
        }
        //更新
        public void Update(ResidentEntity organizeEntity)
        {
            service.Update(organizeEntity);
        }
    }
}
